package com.jingdianjichi.auth.application.controller;

import com.jingdianjichi.auth.application.converter.AuthRolePermissionDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthRolePermissionDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthRolePermissionBO;
import com.jingdianjichi.auth.domain.service.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/*
* 角色权限controller
* */
@RestController
@RequestMapping("/rolePermission")
@Slf4j
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /*
    * 新增角色权限
    * */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("RolePermissionController.add.authRolePermissionDTO:{}",authRolePermissionDTO);
            }
            Objects.requireNonNull(authRolePermissionDTO.getRoleId(),"角色id不能为空");
            Objects.requireNonNull(authRolePermissionDTO.getPermissionIdList(),"权限id集合不能为空");

            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.Instance.DTOConverterBO(authRolePermissionDTO);
            Boolean result = authRolePermissionDomainService.add(authRolePermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RolePermissionController.add.authRolePermissionDTO:{}",e.getMessage(),e);
            return Result.fail("新增角色权限失败");
        }
    }


}
